package com.example.room;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MyApplication instance;
    BookDataBase bookdb;

    EditText edt_name,edt_price,edt_publisher;
    TextView tex_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_insert = findViewById(R.id.btn_insert);
        Button btn_show = findViewById(R.id.btn_show);
        Button btn_delete = findViewById(R.id.btn_delete);
        Button btn_update = findViewById(R.id.btn_update);

        edt_name = findViewById(R.id.edt_name);
        edt_price = findViewById(R.id.edt_price);
        edt_publisher = findViewById(R.id.edt_publisher);
        tex_msg = findViewById(R.id.tex_msg);

        btn_insert.setOnClickListener(this);
        btn_show.setOnClickListener(this);
        btn_delete.setOnClickListener(this);
        btn_update.setOnClickListener(this);

        instance = MyApplication.getInstance();
        bookdb = instance.getBookdb();
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.btn_insert){
            BookDao bookDao = bookdb.bookDao();
            BookInfo bookInfo = new BookInfo();
            bookInfo.setBookName(edt_name.getText().toString());
            bookInfo.setPrice(Float.parseFloat(edt_price.getText().toString()));
            bookInfo.setPublisher(edt_publisher.getText().toString());
            bookDao.insert(bookInfo);
        }else if(id == R.id.btn_show){
            StringBuilder sb = new StringBuilder();
            BookDao bookDao = bookdb.bookDao();
            List<BookInfo> bookInfos = bookDao.showList();
            for (BookInfo bookInfo : bookInfos) {
                sb.append(bookInfo.toString()).append("\r\n");
            }
            tex_msg.setText(sb.toString());
        }else if(id == R.id.btn_delete){
            BookDao bookDao = bookdb.bookDao();
            BookInfo bookInfo = new BookInfo();
            bookInfo.setId(1);
            bookDao.delete(bookInfo);
        }else if(id == R.id.btn_update){
            BookDao bookDao = bookdb.bookDao();
            List<BookInfo> bookInfos = bookDao.queryByName(edt_name.getText().toString());
            if(bookInfos != null && bookInfos.size()>0){
                for (BookInfo bookInfo : bookInfos) {
                    bookInfo.setBookName(edt_name.getText().toString());
                    bookInfo.setPrice(Float.parseFloat(edt_price.getText().toString()));
                    bookInfo.setPublisher(edt_publisher.getText().toString());
                    bookDao.update(bookInfo);
                }
            }
        }
    }
}